package org.skydiveapplibrary.ui;

import org.skydiveapplibrary.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

public class TableStyler
{
	// layout constants
	private static final int TABLE_ROW_LEFT_MARGIN = 6;
	private static final int TABLE_ROW_RIGHT_MARGIN = 6;
	private static final int TABLE_ROW_TOP_MARGIN = 0;
	private static final int TABLE_ROW_BOTTOM_MARGIN = 0;
	private static final int TABLE_ROW_MINIMUM_HEIGHT = 30;
	private static final int TABLE_ROW_PADDING_LEFT = 10;
	private static final int TABLE_ROW_PADDING_RIGHT = 10;
	private static final int TABLE_ROW_PADDING_TOP = 3;
	private static final int TABLE_ROW_PADDING_BOTTOM = 1;
	private static final int TEXT_VIEW_DRAWABLE_PADDING = 5;
	
	public enum TableRowLocation
	{
		First,
		Middle,
		Last,
		Solo
	};
	
	public static void styleTableRow(
			Context context,
			TableRow row,
			TableRowLocation location,
			Boolean useLightTheme)
	{
		if (useLightTheme)
			row.setBackgroundResource(getTableRowBackground(location));
		
		row.setPadding(
				dp2px(context, TABLE_ROW_PADDING_LEFT),
				dp2px(context, TABLE_ROW_PADDING_TOP),
				dp2px(context, TABLE_ROW_PADDING_RIGHT),
				dp2px(context, TABLE_ROW_PADDING_BOTTOM));
		TableLayout.LayoutParams params = new TableLayout.LayoutParams();
		params.setMargins(
				dp2px(context, TABLE_ROW_LEFT_MARGIN),
				dp2px(context, TABLE_ROW_TOP_MARGIN),
				dp2px(context, TABLE_ROW_RIGHT_MARGIN),
				dp2px(context, TABLE_ROW_BOTTOM_MARGIN));
		row.setLayoutParams(params);
		row.setMinimumHeight(dp2px(context, TABLE_ROW_MINIMUM_HEIGHT));
	}
	
	public static void styleTableRowTextView(
			Context context,
			TextView textView,
			boolean rightAligned,
			boolean labelStyle,
			boolean withArrow,
			int weight)
	{
		textView.setGravity(rightAligned ? Gravity.RIGHT | Gravity.CENTER_VERTICAL :
				Gravity.LEFT | Gravity.CENTER_VERTICAL);
		textView.setCompoundDrawablePadding(TEXT_VIEW_DRAWABLE_PADDING);
		if (labelStyle)
		{
			textView.setTextAppearance(context, R.style.LabelTextAppearanceStyle);
		}
		if (withArrow)
		{
			Drawable arrow = context.getResources().getDrawable(R.drawable.right_arrow);
			textView.setCompoundDrawablesWithIntrinsicBounds(null, null, arrow, null);
		}
		LayoutParams rowParams = new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		rowParams.gravity = Gravity.CENTER_VERTICAL;
		rowParams.weight = 1;
		textView.setLayoutParams(rowParams);
	}
	
	public static void styleTableRowImageView(
		Context context,
		ImageView imageView,
		int weight)
	{
		LayoutParams rowParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		rowParams.gravity = Gravity.CENTER_VERTICAL;
		rowParams.weight = 1;
		imageView.setLayoutParams(rowParams);
	}
	
	private static int getTableRowBackground(TableRowLocation location)
	{
		if (location == TableRowLocation.First)
		{
			return R.drawable.form_field_top;
		}
		else if (location == TableRowLocation.Middle)
		{
			return R.drawable.form_field;
		}
		else if (location == TableRowLocation.Last)
		{
			return R.drawable.form_field_bottom;
		}
		else if (location == TableRowLocation.Solo)
		{
			return R.drawable.form_field_solo;
		}
		else
		{
			return R.drawable.form_field;
		}
	}
	
	
	public static int dp2px(Context context, float dp)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dp * scale + 0.5f);
	}
}
